﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PixelWarfare
{
    /// <summary>
    /// A menu item that only consists of a clickable image.
    /// </summary>
    class PictureMenuItem : IMenuItem
    {
        #region Fields

        private Bitmap img;
        private Rectangle bounds;   // The bounds of the image.
        private OnClick clickEvent;

        private bool mouseOver;     // Whether the image has the mouse over it.

        #endregion

        #region Constructors

        /// <summary>
        /// Create a PictureMenuItem with the given parameters.
        /// </summary>
        /// <param name="img">Image of the item.</param>
        /// <param name="x">Centered x coordinate of the item.</param>
        /// <param name="y">Centered y coordinate of the item.</param>
        /// <param name="clicked">Click event to run when the item is clicked.</param>
        public PictureMenuItem(Bitmap img, int x, int y, OnClick clicked)
        {
            this.img = img;

            // Centre the rectangle around the given coordinates.
            bounds = new Rectangle(x - img.Width / 2, y - img.Height / 2, img.Width, img.Height);

            mouseOver = false;
            clickEvent = clicked;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Draw the image centered at the given coordinates.
        /// If the mouse is over the image draw a semi-transparent
        /// gray box over the image.
        /// </summary>
        /// <param name="g">The graphics to drawn on.</param>
        public void Draw(Graphics g)
        {
            g.DrawImage(img, bounds);
            if (mouseOver)
            {
                g.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128, 128)), bounds);
            }

        }

        /// <summary>
        /// Set mouseOver to true if the mouse is moved over the image,
        /// and false otherwise.
        /// </summary>
        /// <param name="x">X coordinate of the mouse.</param>
        /// <param name="y">Y coordinate of the mouse.</param>
        public void MouseMove(int x, int y)
        {
            if (bounds.Contains(x, y))
            {
                mouseOver = true;
            }
            else
            {
                mouseOver = false;
            }
        }

        /// <summary>
        /// If the mouse is pressed check if its on the image,
        /// if so run the clickEvent.
        /// </summary>
        /// <param name="x">X coordinate of the mouse click.</param>
        /// <param name="y">Y coordinate of the mouse click.</param>
        public void MousePress(int x, int y)
        {
            if (bounds.Contains(x, y))
            {
                clickEvent();
            }
        }

        /// <summary>
        /// Nothing needs to happen on update.
        /// </summary>
        public void Update() { }

        #endregion
    }
}
